import getRequest from "@/server/getRequest";
import instance from "@/server/axiosInstance";

const service = async ({headers, method, url, data, responseType, baseURL, transformRequest, params}) => {
    const BASE_URL = await getRequest
    return new Promise((resolve, reject) => {
        instance({
            baseURL: baseURL ? baseURL : BASE_URL.data.ApiUrl,
            headers,
            method,
            url,
            data,
            responseType,
            transformRequest,
            params
        }).then((res) => {
            try {
                const {success} = res
                if (success) {
                    resolve({...res, success: true, message: res.message, data: res.data});
                } else {
                    if(responseType === 'blob') {
                        resolve({...res, success: true, message: res.message, data: res});
                    }else {
                        resolve({ ...res, status: false, message: res.data.message || (res.message), data: res.data });
                    }
                }
            } catch (e) {
                reject({success: false, data: null, message: e})
            }
        })
    })
}

export const post = ({baseURL, url, data, responseType, params, transformRequest}) => {
    return service({
        baseURL,
        url,
        data,
        method: 'post',
        responseType,
        params,
        transformRequest
    })
}

export const get = ({baseURL, url, data, responseType, transformRequest, params}) => {
    return service({
        baseURL,
        url,
        data,
        method: 'get',
        responseType,
        transformRequest,
        params
    })
}


